package com.ruoyi.webs.utils;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

public class JsonMapper
        extends ObjectMapper {
    private static final long serialVersionUID = 1L;
    private static Logger logger = LoggerFactory.getLogger(JsonMapper.class);

    private static JsonMapper mapper;

    public JsonMapper() {
        this(JsonInclude.Include.NON_EMPTY);
    }


    public JsonMapper(JsonInclude.Include include) {
        if (include != null) {
            setSerializationInclusion(include);
        }

        enableSimple();

        disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

//        getSerializerProvider().setNullValueSerializer((JsonSerializer) new Object(this));
//
//
//        registerModule((Module) (new SimpleModule()).addSerializer(String.class, (JsonSerializer) new Object(this)));


        setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    }


    public static JsonMapper getInstance() {
        if (mapper == null) {
            mapper = (new JsonMapper()).enableSimple();
        }
        return mapper;
    }


    public static JsonMapper nonDefaultMapper() {
        if (mapper == null) {
            mapper = new JsonMapper(JsonInclude.Include.NON_DEFAULT);
        }
        return mapper;
    }


    public String toJson(Object object) {
        try {
            return writeValueAsString(object);
        } catch (IOException e) {
            logger.warn("write to json string error:" + object, e);
            return null;
        }
    }


    public <T> T fromJson(String jsonString, Class<T> clazz) {
        if (StringUtils.isEmpty(jsonString)) {
            return null;
        }
        try {
            return (T) readValue(jsonString, clazz);
        } catch (IOException e) {
            logger.warn("parse json string error:" + jsonString, e);
            return null;
        }
    }


    public <T> T fromJson(String jsonString, JavaType javaType) {
        if (StringUtils.isEmpty(jsonString)) {
            return null;
        }
        try {
            return (T) readValue(jsonString, javaType);
        } catch (IOException e) {
            logger.warn("parse json string error:" + jsonString, e);
            return null;
        }
    }


    public JavaType createCollectionType(Class<?> collectionClass, Class... elementClasses) {
        return getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }


    public <T> T update(String jsonString, T object) {
        try {
            return (T) readerForUpdating(object).readValue(jsonString);
        } catch (JsonProcessingException e) {
            logger.warn("update json string:" + jsonString + " to object:" + object + " error.", (Throwable) e);
        } catch (IOException e) {
            logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
        }
        return null;
    }


    public String toJsonP(String functionName, Object object) {
        return toJson(new JSONPObject(functionName, object));
    }


    public JsonMapper enableEnumUseToString() {
        enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
        enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
        return this;
    }


//    public JsonMapper enableJaxbAnnotation() {
//        JaxbAnnotationModule module = new JaxbAnnotationModule();
//        registerModule((Module) module);
//        return this;
//    }


    public JsonMapper enableSimple() {
        configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        return this;
    }


    public ObjectMapper getMapper() {
        return this;
    }


    public static String toJsonString(Object object) {
        return getInstance().toJson(object);
    }


    public static Object fromJsonString(String jsonString, Class<?> clazz) {
        return getInstance().fromJson(jsonString, clazz);
    }


    public static void main(String[] args) {
        List<Map<String, Object>> list = Lists.newArrayList();
//        Map<String, Object> map = Maps.newHashMap();
//        map.put("id", Integer.valueOf(1));
//        map.put("pId", Integer.valueOf(-1));
//        map.put("name", "根节点");
//        list.add(map);
//        map = Maps.newHashMap();
//        map.put("id", Integer.valueOf(2));
//        map.put("pId", Integer.valueOf(1));
//        map.put("name", "你好");
//        map.put("open", Boolean.valueOf(true));
//        list.add(map);
        String json = getInstance().toJson(list);
        System.out.println(json);
    }
}